﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using InfoJetSoft.Service.Entity;

//CID 2081 //2014.01.10 //Begin
namespace InfoJetSoft.Service.Util
{
    public class ListAttachmentsUtil
    {
        //CID 2081 //2014.01.10 //Begin
        public static string[] GetKeptListAttachmentFiles(HttpContext context)
        {
            string fileCount = context.Request.Params["InfoBus_KeptListAttachmentFileCount"];
            if (fileCount != null)
            {
                List<string> fileNames = new List<string>();
                int count = int.Parse(fileCount);
                for (int i = 0; i < count; i++)
                {
                    string filePath = context.Request.Params["InfoBus_KeptListAttachmentFile" + i];
                    if (filePath != null)
                    {
                        string fileName = GetFileName(filePath);
                        fileNames.Add(fileName);
                    }
                }
                return fileNames.ToArray();
            }
            return null;
        }
        //CID 2081 //2014.01.10 //End

        //CID 2081 //2014.01.10 //Begin
        public static HttpPostedFile[] GetNewListAttachmentFiles(HttpContext context)
        {
            List<HttpPostedFile> postedFiles = new List<HttpPostedFile>();
            foreach (string fileKey in context.Request.Files.Keys)
            {
                if (fileKey.StartsWith("InfoBus_ListAttachmentFile_"))
                {
                    HttpPostedFile postedFile = context.Request.Files[fileKey];
                    postedFiles.Add(postedFile);
                }
            }
            return postedFiles.ToArray();
        }
        //CID 2081 //2014.01.10 //End

        //CID 2081 //2014.01.10 //Begin
        private static string GetFileName(string filePath)
        {
            int index = filePath.LastIndexOf("/");
            if (index >= 0)
            {
                string name = filePath.Substring(index + 1);
                return name;
            }
            else
            {
                index = filePath.LastIndexOf("\\");
                if (index >= 0)
                {
                    string name = filePath.Substring(index + 1);
                    return name;
                }
                else
                {
                    return filePath;
                }
            }
        }
        //CID 2081 //2014.01.10 //End

        //CID 2081 //2014.01.10 //Begin
        public static void UpdateListAttachments(Form form)
        {
            if (form.JetContext.ListAttachmentsTable != null)
            {
                foreach (string xpath in form.JetContext.ListAttachmentsTable.Keys)
                { 
                    string[] attachments = form.JetContext.ListAttachmentsTable[xpath] as string[];
                    XmlNode attachmentsNode = DOMUtil.selectANode(form.XmlDoc, xpath, form.XmlNsResolver);
                    if (attachmentsNode != null)
                    {
                        string value = JetConstant.xdListAttachments;
                        foreach (string attachment in attachments)
                        {
                            value = value + "\n" + attachment;
                        }
                        DOMUtil.setNodeValue(attachmentsNode, value);
                    }
                }
            }
        }
        //CID 2081 //2014.01.10 //End
    }
}
//CID 2081 //2014.01.10 //End
